Skip to content
This repository was archived by the owner on Jul 27, 2023. It is now read-only.

Avoid lexing magic command when not an identifier #39

Closed
wants to merge 1 commit into from

Conversation

dhruvmanila
Copy link
Member

@dhruvmanila dhruvmanila commented Jul 24, 2023

Avoid lexing magic command when the prefix is not followed by a valid identifier start character. This is considered as an error by the original implementation:

t.transform_cell("% foo")
# "get_ipython().run_line_magic('', 'foo')\n"

# % foo
# UsageError: Line magic function `%` not found.

A few things to note:

  • For var = % foo, a parse error will be raised while for var = ! foo a lex error will be raised because there's no ! token, it's only valid in != context
  • For ? prefix (? foo), a lex error will be raised while for other prefix a parse error will be raised because there's no ? token

@dhruvmanila
Copy link
Member Author

dhruvmanila commented Jul 24, 2023

Current dependencies on/for this PR:

This comment was auto-generated by Graphite.

@dhruvmanila
Copy link
Member Author

Right, if we don't allow this (current PR), then we can't allow an empty command either:

%
!

Earlier, we were lexing it with an empty string.

I'm kinda leaning towards allowing this while lexing as it's not a parse error from IPython but a usage error which we could highlight and fix using Ruff:

  1. % foo would be:

    MagicCommand {
    	value: " foo",
    	kind: Magic,
    }

    The linter would check if the command value starts with any whitespace and throw a violation. The fix would be to remove them.

  2. % would be:

    MagicCommand {
    	value: "",
    	kind: Magic,
    }

    The linter would check if the command value is empty or not. The fix would be remove the statement.

\cc @MichaReiser @konstin

@konstin
Copy link
Member

konstin commented Jul 24, 2023

can't judge this, i defer to micha

Base automatically changed from dhruv/assign-line-magic to main July 24, 2023 12:14
@dhruvmanila dhruvmanila force-pushed the dhruv/no-ident-line-magic branch from e20f093 to 73517af Compare July 24, 2023 12:19
@MichaReiser
Copy link
Member

I'm kinda leaning towards allowing this while lexing as it's not a parse error from IPython but a usage error which we could highlight and fix using Ruff:

Sounds reasonable to me. It also seems a plus to give consistent error messages

@dhruvmanila
Copy link
Member Author

Sounds reasonable to me. It also seems a plus to give consistent error messages

Thanks! Closing this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants